//
//  HSAFNetTool.m
//  KingNews
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HSAFNetTool.h"
#import "AFNetworking.h"
@implementation HSAFNetTool

+(void)GETNetWithUrl:(NSString *)url
                body:(id)body
          headerFile:(NSDictionary *)header
            response:(HSResponseStyle)responseStyle
             success:(BlockOfSuccess)success
             failure:(void (^)(NSError * error))failure{
    
//    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    
    if (header) {
        
        for (NSString *key in header.allKeys) {
            
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
            
            
        }
        
        
        
        
    }
    
    switch (responseStyle) {
        case HSJSON:
             manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case HSXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case HSDATA:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
            
        default:
            break;
    }
    
     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
     url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    缓存1
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld.plist", (unsigned long)[url hash]]];
//    请求数据
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        if (responseObject) {
         //    缓存2
            [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
         //    缓存3
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (result) {
                success(result);
            }
            
            failure(error);
        }
        
        
    }];




}



+(void)POSTNetWithUrl:(NSString *)url
                body:(id)body
            bodyStyle:(HSRequestStyle)bodyRequestStyle
          headerFile:(NSDictionary *)header
            response:(HSResponseStyle)responseStyle
             success:(BlockOfSuccess)success
             failure:(void (^)(NSError * error))failure{
    
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //
    switch (bodyRequestStyle) {
            
        case HSResponseJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
            
            case HSResponseString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    switch (responseStyle) {
        case HSJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case HSXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case HSDATA:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
            
        default:
            break;
    }
    
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //    请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
        
        
    }];
    
    
    
    
}



@end
